<?php
namespace app\api\controller;

use app\admin\model\arearecord\Arearecord;
use app\admin\model\system\System;
use app\common\controller\Api;
use think\Db;
use think\Exception;

class Area extends Api{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function quzhang(){
        $userModel = new \app\admin\model\User();
        $rechargeModel = new \app\admin\model\recharge\Recharge();

        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();
        $where['district']=['!=',''];
        $list = collection($userModel->where($where)->where(['identity_id'=>2])->select());
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    //取出这个区所有的充值金额
                    $time0=strtotime(date('Ymd',time()));
                    $rwhere['create_time']=['>',$time0];
                    $rwhere['status']=1;
                    $rwhere['district']=$v['district'];
                    $usdt_amount = $rechargeModel->where($rwhere)->where(['unit'=>2])->sum('amount');
                    $hc_amount = $rechargeModel->where($rwhere)->where(['unit'=>1])->sum('amount');
                    $amount = $usdt_amount*$systeminfo['bili']+$hc_amount;
                    if($amount > 0){
                        $one_amount = $amount*0.008;
                        //修改区长的余额
                        $userModel->where(['id'=>$v['id']])->update([
                            'shouyi_balance'=>$v['shouyi_balance']+$one_amount,
                            'quyu_amount'=>$v['quyu_amount']+$one_amount,
                        ]);
                        //写入区域奖纪录
                        $arearecordModel = new Arearecord();
                        $indata['user_id']=$v['id'];
                        $indata['amount']=$one_amount;
                        $indata['event'] = '用户'.$v['nickname'].'成为区长获取0.8%区域分红'.$one_amount;
                        $indata['create_time']=time();
                        $arearecordModel->insert($indata);
                    }
                }
                Db::commit();
                $this->success('发放区长分红成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }else{
            $this->success('成功',[]);
        }
    }

    public function chengzhu(){
        $userModel = new \app\admin\model\User();
        $rechargeModel = new \app\admin\model\recharge\Recharge();

        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();
        $where['city']=['!=',''];
        $list = collection($userModel->where($where)->where(['identity_id'=>3])->select());
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    //取出这个区所有的充值金额
                    $time0=strtotime(date('Ymd',time()));
                    $rwhere['create_time']=['>',$time0];
                    $rwhere['status']=1;
                    $rwhere['city']=$v['city'];
                    $usdt_amount = $rechargeModel->where($rwhere)->where(['unit'=>2])->sum('amount');
                    $hc_amount = $rechargeModel->where($rwhere)->where(['unit'=>1])->sum('amount');
                    $amount = $usdt_amount*$systeminfo['bili']+$hc_amount;
                    if($amount > 0) {
                        $one_amount = $amount*0.005;
                        //修改区长的余额
                        $userModel->where(['id'=>$v['id']])->update([
                            'shouyi_balance'=>$v['shouyi_balance']+$one_amount,
                            'quyu_amount'=>$v['quyu_amount']+$one_amount,
                        ]);
                        //写入区域奖纪录
                        $arearecordModel = new Arearecord();
                        $indata['user_id']=$v['id'];
                        $indata['amount']=$one_amount;
                        $indata['event'] = '用户'.$v['nickname'].'成为城主获取0.5%区域分红'.$one_amount;
                        $indata['create_time']=time();
                        $arearecordModel->insert($indata);
                    }
                }
                Db::commit();
                $this->success('发放城主分红成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }else{
            $this->success('成功',[]);
        }
    }

    /**校准团队总充值额**/
    public function setteamamount(){
        $rechargeModel = new \app\admin\model\recharge\Recharge();
        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();
        $userModel = new \app\admin\model\User();
        $list = collection($userModel->where([])->select());
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    $userlist = collection($userModel->where("FIND_IN_SET({$v['id']}, pids)")->select());
                    if($userlist){
                        $userlist = $userlist->toArray();
                        $idarr=[];
                        foreach ($userlist as $k1=>$v1){
                            array_push($idarr,$v1['id']);
                        }
                        $idstr=implode(',',$idarr);
                        $rwhere['user_id']=['in',$idstr];
                        $amount = $rechargeModel->where($rwhere)->where(['status'=>1])->sum('amount');
                        $team_amount = $amount*$systeminfo['bili'];
                        $userModel->where(['id'=>$v['id']])->update(['team_amount'=>$team_amount]);
                    }
                }
                Db::commit();
                $this->success('成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
    }

    /**校准直推人数**/
    public function setzhitui(){
        $userModel = new \app\admin\model\User();
        $list = collection($userModel->where([])->select());
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    $num = $userModel->where(['pid'=>$v['id']])->count();
                    $userModel->where(['id'=>$v['id']])->update(['zhituinums'=>$num]);
                }
                Db::commit();
                $this->success('成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
    }

    /**校准充值的省市区**/
}